<template>
    <div>
        <h3>发表评论</h3>
        <hr>
        <textarea placeholder="输入评论内容(不能超过120字)" maxlength="120" v-model="msg"></textarea>
        <mt-button type="primary" size="large" @click="postContent">发表评论</mt-button>
        <div class="pinglun-content">
            <div class="pinglun-list" v-for="(item,index) in contents" :key="index">
                <div class="pinglun-list-header">
                    第{{index+1}}楼&nbsp;&nbsp;用户:{{item.user_name}}&nbsp;&nbsp;发表时间:{{item.add_time|dateFormat}}
                </div>
                <div class="pinglun-list-content">
                   {{item.content}}
                </div>
            </div>
        </div>
        <mt-button type="primary" plain size="large" @click="getMore">加载更多</mt-button>
    </div>
</template>
<script>
export default {
    data(){
        return{
            msg:"",
            contents:[],
            pageindex:1
        }
    },
    props:["id"],
    created(){
       this.getContent();
    },
    methods:{
        postContent(){
           var obj = {user_name:"匿名用户",add_time:new Date(),content:this.msg}
           this.$http.post("api/postcomment/"+this.$route.params.id,{content:this.msg}).then((result)=>{
               if(result.body.status===0){
                  var obj = {user_name:"匿名用户",add_time:new Date(),content:this.msg};
                  this.contents.unshift(obj);
                  this.msg="";
               }else{
                   alert("请求失败");
               }
           })
        },
        getMore(){
            this.pageindex++;
            this.getContent()
        },
        getContent(){
          this.$http.get("api/getcomments/"+this.id+"?pageindex="+this.pageindex).then((result)=>{
                  if(result.body.status===0){
                      this.contents=this.contents.concat(result.body.message);
                  }else{
                      alert("请求失败");
                  }
            })
        }
    }
}
</script>
<style>
  .pinglun-content{
    margin-top:10px;
  }
  .pinglun-content .pinglun-list .pinglun-list-header{
    background-color:#ccc;
    line-height: 30px;
    font-size:14px;
  }
  .pinglun-content .pinglun-list .pinglun-list-content{
      padding:10px 0;
  }
</style>

